package com.example.project.dao.impl;

import com.example.project.dao.IEmployeeDao;
import com.example.project.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
 //根据接口操作数据库表的接口规范，对其进行实现@Component
 public class IEmployeeDaoImpl implements IEmployeeDao{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int insertEmployee(Employee employee) {
return jdbcTemplate.update("insert into employee(Id, Name, Role, PartId, PostId, Sex, Age, Salary, Sort) " +
"VALUE (?,?,?,?,?,?,?,?,?)"
,employee.getId()
,employee.getName()
,employee.getRole()
,employee.getPartid()
,employee.getPostid()
,employee.getSex()
,employee.getAge()
,employee.getSalary()
,employee.getSort());
}

@Override
public int deleteEmployee(Employee employee) {
return jdbcTemplate.update("delete from employee " +
"where Id=?",employee.getId());
}

@Override
public List<Employee> getAllEmployee(Employee employee) {
return jdbcTemplate.query("select " +
"Id,Name,Role,Partid,Postid,Sex,Age,Salary,Sort " +
"from employee",new Object[]{},
new BeanPropertyRowMapper<>(Employee.class));
}

@Override
public int updateEmployee(Employee employee) {
return jdbcTemplate.update("update employee set " +
"Name=?,Role=?,Partid=?,Postid=?,Sex=?,Age=?,Salary=?,Sort=?" +"where Id=?"

,employee.getName()
,employee.getRole()
,employee.getPartid()
,employee.getPostid()
,employee.getSex()
,employee.getAge());
}

}
